﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using kim.Common;
using System.Data;
namespace kim
{
    partial class AddorEditNews : kim.UI.ManagePage
    {

        protected string type;
        protected int typeid, fid;
        private int Nid;
        protected void Page_Load(object sender, EventArgs e)
        {
            type = KIMRequest.GetString("type");
            typeid = KIMRequest.GetInt("typeid", 0);
            Nid = KIMRequest.GetInt("id", 1);
            fid = KIMRequest.GetInt("fid", 1);

            if (!IsPostBack)
                GetRequestValues();
            Label1.Text = "新闻中心";
            isShow();
        }

        private void isShow()
        {
            if (fid == 1)
            {
                jj.Visible = false;
                upimg.Visible = false;
                EnTitle.Visible = false;
               
            }

            if (fid == 2||fid==7)
            {
                jj.Visible = false;
                upimg.Visible = false;
                EnTitle.Visible = false;
              
            }
            if (fid == 3)
            {
                if (typeid == 1)
                {
                    jj.Visible = true;
                    upimg.Visible = false;
                    EnTitle.Visible = false;
                   
                }
                if (typeid == 2)
                {
                    jj.Visible = true;
                    upimg.Visible = true;
                    xxt.Visible = true;
                    EnTitle.Visible = false;
                   
                }
                if (typeid == 3)
                {
                    jj.Visible = true;
                    upimg.Visible = true;
                    EnTitle.Visible = false;
                   
                }
            }
            if (fid == 4 )
            {
                if (typeid == 1)
                {
                    jj.Visible = false;
                    upimg.Visible = false;
                    EnTitle.Visible = false;
                }
                if (typeid == 2)
                {
                    jj.Visible = true;
                    upimg.Visible = true;
                    xxt.Visible = true;
                    EnTitle.Visible = false;
                }
            } 
            if (fid == 5||fid==6)
            {
                if (typeid == 1)
                {
                    jj.Visible = false;
                    upimg.Visible = true;
                    EnTitle.Visible = false;
                }
                if (typeid == 2)
                {
                    jj.Visible = true;
                    upimg.Visible = true;
                    xxt.Visible = true;
                    EnTitle.Visible = false;
                }
                if (typeid == 3)
                {
                    jj.Visible = false;
                    upimg.Visible = true;
                    EnTitle.Visible = false;
                }
            }

        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (type == KIMEnums.ActionEnum.Edit.ToString())
            {
                if (DoEdit(Nid))
                    lbmsg.Text = "修改成功";
                else
                    lbmsg.Text = "修改失败";
            }
            if (type == KIMEnums.ActionEnum.Add.ToString())
            {
                if (DoAdd())
                    lbmsg.Text = "添加成功";
                else
                    lbmsg.Text = "添加失败";
            }

        }

        #region 编辑添加基本操作=========================================

        private bool DoEdit(int id)
        {
            Model.eg_news Mnew = new Model.eg_news();
            Mnew.Sort = int.Parse(txt_postion.Value.Trim());
            Mnew.Content = txt_Content.Value;
            Mnew.ClassID = typeid;
            Mnew.ChannelID = fid;
            Mnew.zjj = this.Headpic2.Src;

            Mnew.EnName = txtEnTitle.Value;
            if (this.txtTime.Value != "")
                Mnew.InTime = Convert.ToDateTime(this.txtTime.Value);
            else
                Mnew.InTime = DateTime.Now;
            Mnew.Name = txtTitle.Value;
            Mnew.MaxPic = txtImgUrl_max.Value;
            Mnew.Pic = txtImgUrl_min.Value;
            Mnew.ID = id;
            Mnew.jj = TxtJJ.Value;
            if (CbIsShow.Checked)
                Mnew.DesignerID = 1;
            else
                Mnew.DesignerID = 0;
            Headpic.Src = txtImgUrl_min.Value;
            return new BLL.eg_news().Update(Mnew);
        }

        private bool DoAdd()
        {

            Model.eg_news Mnew = new Model.eg_news();
            if (this.txtTime.Value != "")
                Mnew.InTime = Convert.ToDateTime(this.txtTime.Value);
            else
                Mnew.InTime = DateTime.Now;
            Mnew.jj = TxtJJ.Value;
            Mnew.zjj = this.Headpic2.Src;
            Mnew.Sort = int.Parse(txt_postion.Value.Trim());
            Mnew.Content = txt_Content.Value;
            Mnew.ClassID = typeid;
            Mnew.EnName = txtEnTitle.Value;
            Mnew.MaxPic = txtImgUrl_max.Value;
            Mnew.Pic = txtImgUrl_min.Value;
            Mnew.ChannelID = fid;
            Mnew.Name = txtTitle.Value;
            if (CbIsShow.Checked)
                Mnew.DesignerID = 1;
            else
                Mnew.DesignerID = 0;
            Headpic.Src = txtImgUrl_min.Value;
            BLL.eg_news bll = new BLL.eg_news();
            if (bll.Add(Mnew) > 0)
                return true;
            else
                return false;
        }
        #endregion

        private void GetRequestValues()
        {

            if (type == KIMEnums.ActionEnum.Edit.ToString())
            {
                GetEditValue();
            }
            if (type == KIMEnums.ActionEnum.Delete.ToString())
            {
                bool del = new BLL.eg_news().Delete(Nid);
                if (del)
                    Response.Redirect("List.aspx?typeid=" + typeid + "&fid=" + fid);
            }
            if (type != KIMEnums.ActionEnum.Edit.ToString() && type != KIMEnums.ActionEnum.Add.ToString() && type != KIMEnums.ActionEnum.Delete.ToString())
                Response.Redirect("../indexpage.aspx");
            else
            {
                //lb_po.Text = new BLL.eg_news().GetMaxSort("").ToString();
            }
        }

        private void GetEditValue()
        {
            this.txtTime.Value = DateTime.Now.ToString("yyyy-MM-dd");
            Model.eg_news model = new BLL.eg_news().GetModel(Nid);
            this.txtTitle.Value = model.Name;
            this.txt_postion.Value = model.Sort.ToString();
            Headpic.Src = model.Pic;
            this.Headpic2.Src = model.zjj;
            if (model.DesignerID == 1)
                CbIsShow.Checked = true;
            else
                CbIsShow.Checked = false;
            if (model.InTime != null)
                this.txtTime.Value = ((DateTime)model.InTime).ToString("yyyy-MM-dd");
            this.TxtJJ.Value = model.jj;
            this.txtEnTitle.Value = model.EnName;
            this.txtImgUrl_min.Value = model.Pic;
            txt_Content.Value = model.Content;
            this.txtImgUrl_max.Value = model.MaxPic;
        }
        protected void btn_sc2_Click(object sender, EventArgs e)
        {
            string strpic = PubFileAndImageFunc.fileuploadcode(this.FileUpload2, "../../pic/");
            ViewState["img2"] = strpic != "" ? strpic : this.Headpic2.Src.ToString();
            this.Headpic2.Src = ViewState["img2"].ToString();
        }
    }
}